﻿using UnityEngine;

namespace Core
{
    /// <summary>
    /// 俯视角摄像机
    /// 可以拖动，放大缩小
    /// 不能转向
    /// </summary>
    public class CameraTopDown : Core.Component, IAwake, IUpdate, ILateUpdate
    {
        public bool Enabled;

        public float DragSpeed = 0.1f;
        
        private Vector3 dragOrigin;

        public void Awake()
        {
            Enabled = true;
        }

        public void LateUpdate()
        {
            if (Enabled == false || Camera.main == null)
            {
                return;
            }

            //var transform = Camera.main.transform;

            //// 计算目标位置在屏幕上的坐标
            //Vector3 pos = Camera.main.WorldToViewportPoint(target.position);

            //if (Within(pos.x, range) && Within(pos.y, range))
            //{
            //    return;
            //}

            //pos.x = Mathf.Clamp(pos.x, range.x, range.y);
            //pos.y = Mathf.Clamp(pos.y, range.x, range.y);

            //Vector3 delta = target.position - Camera.main.ViewportToWorldPoint(pos);

            //Vector3 destination = transform.position + delta;

            //transform.position = destination;
            ////transform.position = Vector3.Lerp(transform.position, destination, smoothSpeed * Time.deltaTime);

            Drag();
        }

        private void Drag()
        {
            var transform = Camera.main.transform;
            var orthographicSize = Camera.main.orthographicSize;

            if (Input.GetMouseButton((int)0))
            {
                float x = Input.GetAxis("Mouse X") * DragSpeed;
                float y = Input.GetAxis("Mouse Y") * DragSpeed;

                //if (linkedZoomDrag)
                {
                    x *= orthographicSize;
                    y *= orthographicSize;
                }

                transform.Translate(-x, -y, 0);
            }

        }

        public void Update()
        {
            //if (enabled == false || Camera.main == null)
            //{
            //    return;
            //}

            //var transform = Camera.main.transform;

            //if (Input.GetMouseButtonDown(0))
            //{
            //    dragOrigin = Input.mousePosition;
            //    return;
            //}

            //if (!Input.GetMouseButton(0)) return;

            //Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
            //Vector3 move = new Vector3(-pos.x * dragSpeed, -pos.y * dragSpeed, 0);

            //transform.Translate(move, Space.World);
        }
    }
}
